﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using Phys;
using System.ComponentModel;
using Phys.UI.Properties;
using Phys.Loader;
using Path = System.IO.Path;
namespace Phys.UI {
	public partial class MainWindow : Window {
		public MainWindow() {
			InitializeComponent();
		}
		PhysicsGLWindow gl;
		private void Window_Loaded(object sender, RoutedEventArgs e) {
			if (System.Diagnostics.Debugger.IsAttached) {
				Start(@"..\Worlds\WorldCollision.xml");
				//gl.World.Start(); // start physics
				// WindowState = WindowState.Minimized; // minimize the window
				Visibility = Visibility.Collapsed; // hide the window
			}
		}

		private void PhysicsToggle_Checked(object sender, RoutedEventArgs e) {
			if (gl == null || gl.World == null) return;
			gl.World.Start();
		}
		private void PhysicsToggle_Unchecked(object sender, RoutedEventArgs e) {
			if (gl == null || gl.World == null) return;
			gl.World.Stop();
		}

		private void Window_Closed(object sender, EventArgs e) {
			if (gl != null) gl.Close();
		}

		private void load_Click(object sender, RoutedEventArgs e) {
			Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
			dlg.FileName = Settings.Default.LastWorldFile;
			dlg.DefaultExt = ".xml";
			dlg.AddExtension = true;
			dlg.Filter = "World descriptors (.xml)|*.xml";
			if (dlg.ShowDialog() ?? false) {
				string filename = dlg.FileName;
				Start(filename);
			}
		}

		/// <summary>
		/// Starts a new OpenGL window with the specified XML world.
		/// </summary>
		/// <param name="filename">The XML file to load as World</param>
		private void Start(string filename) {
			Settings.Default.LastWorldFile = filename;
			if (gl != null) gl.Close();
			gl = new PhysicsGLWindow();
			Console.WriteLine("Loading " + filename);
			gl.Start(filename);
		}
	}
}
